package com.typhosoft.acommon.network;

import android.net.Uri;
import android.widget.ImageView;

import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.Connection;
import com.typhosoft.jcommon.bean.CPhoto;
import com.typhosoft.jcommon.photo.IPathProvider;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by Mario on 24/03/2014.
 */
public class CPhotoConsumer extends com.typhosoft.jcommon.network.CDefaultPhotoConsumer {

    private final ImageView imageView;

    public CPhotoConsumer(ImageView imageView, IPathProvider pathProvider) {
        super(pathProvider);
        this.imageView= imageView;
    }

    @Override
    public void disconnected(Connection connection) {
        super.disconnected(connection);

        final Connection conn = connection;
        new Thread() {
            public void run () {
                try {
                    Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Reconnecting...");
                    ((Client)conn).reconnect();
                } catch (IOException ex) {
                    Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Error during reconnection.", ex);
                }
            }
        }.start();
    }

    @Override
    public void writePhoto(CPhoto photo, String path) throws IOException {
        super.writePhoto(photo, path);
        imageView.setImageURI(Uri.fromFile(new File(path)));
    }
}
